<?php
include 'sms.php';

class ccnsms extends csms{
	var $lastrecvtime = '';//上次接收到最近一条回复短信的回复时间, 用以迭代取回复短信.
	
	//服务器地址
	var $server = "http://http.c123.com";
	
	function ccnsms($u, $p){
		$this->user = $u;
		$this->passwd = mymd5($p);
	}
	
	function curmoney(){
		return $this->cursmscount();
	}
	
	function cursmscount(){
		$res = readfromurl("{$this->server}/mm/?uid={$this->user}&pwd={$this->passwd}");
		if(strpos($res, "100")===0){
			$count = substr($res, strlen("100||"));
			return (int)$count;
		}
		else{
			mylog ( "获取短信剩余数目时出现错误, 返回代码为:$res." );
			
			if ("101" == $res) { //用户名密码错误
				return ESMS_UPERROR;
			} elseif ("109" == $res) {
				return ESMS_USERCLOSE;
			} else {
				return ESMS_UNKNOWN;
			}
		}
	}
	
	function sendsms($tonum, $content, $sendtime="", $fromnum = ""){
		if(null==$content || ""==$content) return ESMS_CONTENTERR;
		if(null==$tonum || ""==$tonum || count($tonum)==0) return ESMS_EMPTYTO;
		
		//构造客户号码串及发送结果
		if(is_array($tonum)){
			$tonumstr = '';
			foreach ($tonum as $to){
				if(empty($to)) continue;
				$tonumstr .= "$to,";
				
				$sms = array('from'=>'', 'to'=>$to, 'content'=>$content, 'sended'=>time());
				$smses[] = $sms;
			}
		}
		else{
			$tonumstr = $tonum;
		}
		
		$pcontent = utf8togbk($content);//TODO 解析内容, 处理禁发关键字
		
		//TODO 处理时间
		$sendtime = "";
		
		//组合参数
		$params = array(
			 "uid"=>$this->user
			,"pwd"=>$this->passwd
			,"mobile"=>$tonumstr
			,"content"=>$pcontent
			//,"time"=>$sendtime
			);
		
		//调用接口发送
		//readfromurl("{$this->server}/tx/?username={$this->user}&password={$this->passwd}&smsnumber=$sendto&smscontent=$content&sendtime=$sendtime");
		$res = postrequest("{$this->server}/tx/", $params);
		$res = $res['result'];
		
		if(strpos($res, "100")===0){//发送成功
			return $smses;
		}
		else{
			mylog("发送短信出现错误, 返回的状态码:$res.");
			
			if("101"==$res){//用户名密码错误
				return ESMS_UPERROR;
			}
			elseif("109"==$res){
				return ESMS_USERCLOSE;
			}
			elseif("102"==$res){
				return ESMS_NOMONEY;
			}
			elseif("108"==$res || "112"==$res){
				return ESMS_TONUMERR;
			}
			elseif("105"==$res){
				return ESMS_CONTENTERR;
			}
			elseif("106"==$res){
				return ESMS_MANYTONUM;
			}
			elseif("104"==$res){//含有屏蔽字
				//TODO 处理禁发关键字
			}
			else{
				return ESMS_UNKNOWN;
			}
		}
	}
	
	function recvsms(){
		$starttime = $this->lastrecvtime;
		$endtime = "";
		
		$params = array(
			 "uid"=>$this->user
			,"pwd"=>$this->passwd
			//,"time"=>$starttime
			);
		$res = postrequest("{$this->server}/rx/", $params);
		$res = $res['result'];
		
		if(@strpos($res, "100")===0){//获得回复的短信
			$smses = array();
			$res = substr($res, strlen("100{&}"));
			if(null==$res || ""==$res) return $smses;
			
			$smsesstr = explode("{&}", $res);
			foreach ($smsesstr as $smsstr){
				if(null==$smsstr || ""==$smsstr) continue;
				
				$parts = explode("||", $smsstr);
				$sms['from'] = $parts[0];
				$sms['sended'] = parsedate($parts[2]);
				$sms['content'] = gbktoutf8($parts[1]);
				$sms['to'] = '';
				
				$smses[] = $sms;
			}
			
			return $smses;
		}
		else{
			mylog("收取回复短信出现错误, 返回的状态码:$res.");
		
			if("101" == $res) { //用户名密码错误
				return ESMS_UPERROR;
			} elseif ("109" == $res) {
				return ESMS_USERCLOSE;
			} else {
				return ESMS_UNKNOWN;
			}
		}
	}
}
//
//$user = "133592";
//$password = "tian0703";
//
//$sms = new ccnsms($user, $password);
////echo $sms->sendsms(array("18618160032", "18210913063"), "测试, 请回复:)");
//echo print_r($sms->recvsms(), true);


//$mobile = "18618160032";//"13817940167";//"18210913063";//"18618160032";
//$content = urlencode(utf8togbk("测试移动, 请回复:)"));
//发送
//$res = readfromurl("http://http.c123.com/tx/?uid=$user&pwd=$password&mobile=$mobile&content=$content");
//echo $res;

//回复
//$res = readfromurl("http://http.c123.com/rx/?uid=$user&pwd=$password");
//echo gbktoutf8($res);

//剩余条数
//$res = readfromurl("http://http.c123.com/mm/?uid=$user&pwd=1$password");
//echo ($res);
?>